<?php
/**
 * 添加项目
 * @author jianhui
 *
 */
class SingleProject extends MY_Controller{
    function SingleProject(){
        parent::MY_Controller();
        //设置导航选中
        $this->navbarFocus = 'SingleBudget';
    }
    /**
     * 项目添加
     */
    function add(){
        $this->load->model('sProjectModel');
        if ($_POST){
            $data = array(
                'storeId'       => $this->user['userId'],
                'procId'        => (int)$this->input->post('procId'),
                'isBubao'       => (int)$this->input->post('isBubao'),
                'projectName'   => $this->input->post('projectName'),
                'actionType'    => (int)$this->input->post('actionType'),
                'actionAlias'   => $this->input->post('actionAlias'),
                'eventType'     => (int)$this->input->post('eventType'),
                'eventClass'    => (int)$this->input->post('eventClass'),
                'eventClassAlias'=> $this->input->post('eventClassAlias'),
                'createTime'    => time(),
            );
            $data['storeId'] || showError('请填入店铺ID');
            $data['procId'] || showError('请选择主线流程ID');
            $data['projectName'] || showError('请输入项目名称');
            //验证标准动作
            $data['actionType'] || showError('请选择标准动作');
            $this->load->model('actionModel');
            $actionData = $this->actionModel->getData($data['actionType']);
            if (in_array($actionData['name'], array('其他','其它')) && !$data['actionAlias']){
                showError('请输入标准动作其他值');
            }
            $data['eventType'] || showError('请选择活动类型');
            $data['eventClass'] || showError('请选择活动分类');
            if($data['eventClass'] == -1 && !$data['eventClassAlias']){
                showError('请输入活动分类其他值');
            }
            $projectId = $this->sProjectModel->add($data);
            if ($projectId){
                $this->sProjectModel->updateMoney($projectId);
                showSuccess('添加成功', printUrl('SingleProject', 'childDetail', array('procId'=>$data['procId'], 'projectId'=>$projectId)));
            }else{
                showError('添加项目失败');
            }
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择要添加项目的流程');
            $this->viewData['procId'] = $procId;
            $isBubao = (int)$this->input->get('isBubao');
            $this->viewData['isBubao'] = $isBubao;
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleBudget'.($isBubao?'Again':''), 'storeList'),'title'=>($isBubao?'补报':'').'预算申请列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleBudget'.($isBubao?'Again':''), 'storeEdit', array('procId'=>$procId)),'title'=>'百县强基列表');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'百县强基添加');
            //如果为补报，切换导航栏选中状态
            $isBubao && $this->navbarFocus = 'SingleBudgetAgain';
            //主线流程数据
            $this->load->model('singleProcessModel');
            $procData = $this->singleProcessModel->getNewData(array('id'=>$procId));
            $this->load->model('actionModel');
            //默认基金类别为单店
            $type = 1;
            $this->viewData['actionList'] = $this->actionModel->getActiveList($procData['timeUnit'], $type, $procData['year']);
            $this->load->view('SingleProject/add', $this->viewData);
        }
    }
    /**
     * 项目添加
     */
    function edit(){
        $this->load->model('sProjectModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId'       => $this->user['userId'],
                'procId'        => (int)$this->input->post('procId'),
                'projectName'   => $this->input->post('projectName'),
                'actionType'    => (int)$this->input->post('actionType'),
                'actionAlias'   => $this->input->post('actionAlias'),
                'eventType'     => (int)$this->input->post('eventType'),
                'eventClass'    => (int)$this->input->post('eventClass'),
                'eventClassAlias'=> $this->input->post('eventClassAlias'),
            );
            $data['storeId'] || showError('请填入店铺ID');
            $data['procId'] || showError('请选择主线流程ID');
            $data['projectName'] || showError('请输入项目名称');
            //验证标准动作
            $data['actionType'] || showError('请选择标准动作');
            $this->load->model('actionModel');
            $actionData = $this->actionModel->getData($data['actionType']);
            if (in_array($actionData['name'], array('其他','其它')) && !$data['actionAlias']){
                showError('请输入标准动作其他值');
            }
            $data['eventType'] || showError('请选择活动类型');
            $data['eventClass'] || showError('请选择活动分类');
            if($data['eventClass'] == -1 && !$data['eventClassAlias']){
                showError('请输入活动分类其他值');
            }
            $projectId = $this->sProjectModel->edit($id,$data);
            if ($projectId){
                $this->sProjectModel->updateMoney($projectId);
                showSuccess('修改成功', printUrl('SingleProject', 'childDetail', array('procId'=>$data['procId'], 'projectId'=>$projectId)));
            }else{
                showError('修改项目失败');
            }
        }else{
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择要修改的项目');
            $this->viewData['projectId'] = $projectId;
            $this->load->model('sProjectModel');
            $projectData = $this->sProjectModel->getNewData(array('id'=>$projectId,'storeId'=>$this->user['userId']));
            $projectData || showError('项目数据不存在');
            $this->viewData['projectData'] = $projectData;
            in_array($projectData['budgetAuditRs'], array(-1,0,3)) || showError('项目已审核，不允许修改');
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleBudget'.($projectData['isBubao']?'Again':''), 'storeList'),'title'=>($projectData['isBubao']?'补报':'').'预算申请列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleBudget'.($projectData['isBubao']?'Again':''), 'storeEdit', array('procId'=>$this->viewData['projectData']['procId'])),'title'=>'百县强基列表');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'百县强基修改');
            //如果为补报，切换导航栏选中状态
            $projectData['isBubao'] && $this->navbarFocus = 'SingleBudgetAgain';
            //主线流程数据
            $this->load->model('singleProcessModel');
            $procData = $this->singleProcessModel->getNewData(array('id'=>$this->viewData['projectData']['procId']));
            //默认基金类别为单店
            $type = 1;
            $this->load->model('actionModel');
            $this->viewData['actionList'] = $this->actionModel->getActiveList($procData['timeUnit'], $type, $procData['year']);
            $this->viewData['eventType'] = $this->config->item('projectEventType');
            $this->load->view('SingleProject/edit', $this->viewData);
        }
    }
    /**
     * 项目删除
     */
    function del(){
        $projectId = (int)$this->input->post('projectId');
        $projectId || showError('请选择要删除的项目ID');
        $this->load->model('sProjectModel');
        $projectData = $this->sProjectModel->getNewData(array('id'=>$projectId,'storeId'=>$this->user['userId']));
        in_array($projectData['budgetAuditRs'], array(-1,0,3)) || showError('项目已审核，不允许修改');
        if ($projectId){
            $this->load->model('sPODetailModel');
            $this->sPODetailModel->del(array('projectId'=>$projectId,'storeId'=>$this->user['userId']),'*');
            $this->load->model('sPOfflineModel');
            $this->sPOfflineModel->del(array('projectId'=>$projectId,'storeId'=>$this->user['userId']),'*');
            $this->load->model('sPGxModel');
            $this->sPGxModel->del(array('projectId'=>$projectId,'storeId'=>$this->user['userId']),'*');
            $this->sProjectModel->del(array('id'=>$projectId,'storeId'=>$this->user['userId']) );
            showSuccess('删除项目成功');
        }else{
            showError('删除项目失败');
        }
    }
    /**
     * 项目子类详细，用于编辑信息
     */
    function childDetail(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择主线序号');
        $this->viewData['procId'] = $procId;
        $projectId = (int)$this->input->get('projectId');
        $projectId || showError('请选择项目');
        $this->viewData['projectId'] = $projectId;
        //项目模型
        $this->load->model('sProjectModel');
        $projectData = $this->sProjectModel->getNewData(array('id'=>$projectId,'storeId'=>$this->user['userId']));
        $projectData || showError('项目数据不存在');
        $this->viewData['projectData'] = $projectData;
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleBudget'.($projectData['isBubao']?'Again':''), 'storeList'),'title'=>($projectData['isBubao']?'补报':'').'预算申请列表');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleBudget'.($projectData['isBubao']?'Again':''), 'storeEdit', array('procId'=>$procId)),'title'=>'百县强基列表');
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'百县强基明细');
        //如果为补报，切换导航栏选中状态
        $projectData['isBubao'] && $this->navbarFocus = 'SingleBudgetAgain';
        //金额明细
        $this->viewData['moneyDetail'] = $this->sProjectModel->getDetailMoney($projectId);
        //线上广宣
        $this->load->model('sPGxModel');
        $this->viewData['gxList'] = $this->sPGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId));
        //线下活动
        $this->load->model('sPOfflineModel');
        $this->viewData['offlineList'] = $this->sPOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId));
        $this->viewData['action'] = 'edit';
        $this->load->view('SingleProject/childDetail', $this->viewData);
    }
    /**
     * 预算查看
     */
    function budgetCheck(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择主线序号');
        $this->viewData['procId'] = $procId;
        $projectId = (int)$this->input->get('projectId');
        $projectId || showError('请选择项目');
        $this->viewData['projectId'] = $projectId;
        //项目模型
        $this->load->model('sProjectModel');
        $this->viewData['projectData'] = $this->sProjectModel->getNewData(array('id'=>$projectId));
        $this->viewData['projectData'] || showError('项目数据不存在');
        $this->viewData['moneyDetail'] = $this->sProjectModel->getDetailMoney($projectId);
        //线上广宣
        $this->load->model('sPGxModel');
        $this->viewData['gxList'] = $this->sPGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId));
        //线下活动
        $this->load->model('sPOfflineModel');
        $this->viewData['offlineList'] = $this->sPOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId));
        //查看标记
        $this->viewData['action'] = 'check';
        $this->load->view('SingleProject/budgetCheck', $this->viewData);
    }
    /**
     * 项目预算提交
     */
    function budgetSubmit(){
        $procId = (int)$this->input->post('procId');
        $procId || showError('请选择主线数据');
        $projectId = (int)$this->input->post('projectId');
        $projectId || showError('请选择要提交预算申请的项目');
        $this->load->model('sProjectModel');
        $projectData = $this->sProjectModel->getData(array('id'=>$projectId, 'storeId'=>$this->user['userId']));
        $projectData || showError('项目数据不存在');
        if ($this->sProjectModel->edit($projectId, array('budgetAuditRs'=>0,'budgetSubTime'=>time()))){
            $this->load->model('sPGxModel');
            $this->load->model('sPOfflineModel');
            $this->sPGxModel->edit(array('projectId'=>$projectId,'storeId'=>$this->user['userId'],'budgetAuditRs'=>-1), array('budgetAuditRs'=>0,'budgetSubTime'=>time()), '*');
            $this->sPOfflineModel->edit(array('projectId'=>$projectId,'storeId'=>$this->user['userId'],'budgetAuditRs'=>-1), array('budgetAuditRs'=>0,'budgetSubTime'=>time()), '*');
            //更新金额
            $this->sProjectModel->updateMoney($projectId);
            showSuccess('提交项目预算申请成功', printUrl('SingleBudget'.($projectData['isBubao']?'Again':''), 'storeEdit', array('procId'=>$procId)));
        }else{
            showError('提交项目预算申请失败');
        }
    }
    /**
     * 预算审核
     */
    function budgetAudit(){
        $this->load->model('sProjectModel');
        $this->load->model('sPGxModel');
        $this->load->model('sPOfflineModel');
        if ($_POST){
            $gxData = $this->input->post('gx');
            $offlineData = $this->input->post('offline');
            if (!$gxData && !$offlineData){
                showError('请还未选择审核项');
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    $this->sPGxModel->budgetAudit((int)$k, (int)$v['budgetAuditRs'], $this->user);
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->sPOfflineModel->budgetAudit((int)$k, (int)$v['budgetAuditRs'], $this->user);
                }
            }
            $projectId = (int)$this->input->get('projectId');
            $this->sProjectModel->edit($projectId, array('budgetAuditDetail'=>1,'budgetAuditRole'=>$this->user['userRole']));
            $this->sProjectModel->budgetUpdateStatus($projectId);
            //更新项目金额
            $this->sProjectModel->updateMoney($projectId);
            //插入审核日志
            $this->load->model('sAuditLog');
            $this->sAuditLog->addLog(2, array(
                'targetId'=>$projectId,
                'userRole'=>$this->user['userRole'],
                'userId' => $this->user['userId'],
                'auditRs' => $this->sProjectModel->getBudgetStatus($projectId),
                'auditDes' => '项目明细审核'
            ));
            showSuccess('审核成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择主线序号');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择项目');
            $this->viewData['projectId'] = $projectId;
            //项目模型
            
            $this->viewData['projectData'] = $this->sProjectModel->getNewData(array('id'=>$projectId));
            $this->viewData['projectData'] || showError('项目数据不存在');
            $this->viewData['moneyDetail'] = $this->sProjectModel->getDetailMoney($projectId);
            //线上广宣
            $this->viewData['gxList'] = $this->sPGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetApply >'=>0));
            //线下活动
            $this->viewData['offlineList'] = $this->sPOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetApply >'=>0));
            //查看标记
            $this->viewData['action'] = 'audit';
            $this->load->view('SingleProject/budgetAudit', $this->viewData);
        }
    }
    /**
     * 结算申请
     */
    function settleSubmit(){
        $this->load->model('sProjectModel');
        $this->load->model('sPGxModel');
        $this->load->model('sPOfflineModel');
        if ($_POST){
            $gxData = $this->input->post('gx');
            $offlineData = $this->input->post('offline');
            if (!$gxData && !$offlineData){
                showError('您的结算申请数据为空');
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    if ((int)$v['isSettle']){
                        (float)$v['settleApply'] >= 0 || showError('编号为【'.$k.'】的广宣实际费用为空');
                        (int)$v['fileId'] || showError('编号为【'.$k.'】的广宣自证照片为空');
                    }
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    if ((int)$v['isSettle']){
                        (float)$v['settleApply'] >= 0 || showError('编号为【'.$k.'】的线下活动实际费用为空');
                        (int)$v['fileId'] || showError('编号为【'.$k.'】的线下活动自证照片为空');
                    }
                }
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    $this->sPGxModel->edit((int)$k, array(
                        'settleAuditRole' => 0,
                        'settleAuditRs'=>(int)$v['isSettle'] ? 0 : -1,
                        'isSettle'=>(int)$v['isSettle'],
                        'fileId' => (int)$v['fileId'],
                        'settleApply' => (float)$v['settleApply'],
                        'settleSubTime' => time()
                    ));
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->sPOfflineModel->edit((int)$k, array(
                        'settleAuditRole' => 0,
                        'settleAuditRs'=>(int)$v['isSettle'] ? 0 : -1,
                        'isSettle'=>(int)$v['isSettle'],
                        'fileId' => (int)$v['fileId'],
                        'settleApply' => (float)$v['settleApply'],
                        'settleSubTime' => time()
                    ));
                }
            }
            $projectId = (int)$this->input->post('projectId');
            $this->sProjectModel->edit($projectId, array(
                'settleSubTime'=>time(),
            ));
            $this->sProjectModel->settleUpdateStatus($projectId);
            $this->sProjectModel->updateMoney($projectId);
            showSuccess('提交结算申请成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择主线序号');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择项目');
            $this->viewData['projectId'] = $projectId;
            //项目模型
            $this->viewData['projectData'] = $this->sProjectModel->getNewData(array('id'=>$projectId));
            $this->viewData['projectData'] || showError('项目数据不存在');
            $this->viewData['moneyDetail'] = $this->sProjectModel->getDetailMoney($projectId);
            //线上广宣
            $this->viewData['gxList'] = $this->sPGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
            //线下活动
            $this->viewData['offlineList'] = $this->sPOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
            //查看标记
            $this->viewData['action'] = 'edit';
            $this->load->view('SingleProject/settleSubmit', $this->viewData);
        }
    }
    /**
     * 预算查看
     */
    function settleCheck(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择主线序号');
        $this->viewData['procId'] = $procId;
        $projectId = (int)$this->input->get('projectId');
        $projectId || showError('请选择项目');
        $this->viewData['projectId'] = $projectId;
        //项目模型
        $this->load->model('sProjectModel');
        $this->viewData['projectData'] = $this->sProjectModel->getNewData(array('id'=>$projectId));
        $this->viewData['projectData'] || showError('项目数据不存在');
        $this->viewData['moneyDetail'] = $this->sProjectModel->getDetailMoney($projectId);
        //线上广宣
        $this->load->model('sPGxModel');
        $this->viewData['gxList'] = $this->sPGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
        //线下活动
        $this->load->model('sPOfflineModel');
        $this->viewData['offlineList'] = $this->sPOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
        //查看标记
        $this->viewData['action'] = 'check';
        $this->load->view('SingleProject/settleCheck', $this->viewData);
    }
    /**
     * 结算审核
     */
    function settleAudit(){
        $this->load->model('sProjectModel');
        $this->load->model('sPGxModel');
        $this->load->model('sPOfflineModel');
        if ($_POST){
            $gxData = $this->input->post('gx');
            $offlineData = $this->input->post('offline');
            if (!$gxData && !$offlineData){
                showError('请还未选择审核项');
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    $this->sPGxModel->settleAudit((int)$k, (int)$v['settleAuditRs'], $this->user);
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->sPOfflineModel->settleAudit((int)$k, (int)$v['settleAuditRs'], $this->user);
                }
            }
            $projectId = (int)$this->input->get('projectId');
            $this->sProjectModel->edit($projectId, array('settleAuditDetail'=>1,'settleAuditRole'=>$this->user['userRole']));
            $this->sProjectModel->settleUpdateStatus($projectId);
            //更新项目金额
            $this->sProjectModel->updateMoney($projectId);
            //插入审核日志
            $this->load->model('sAuditLog');
            $this->sAuditLog->addLog(4, array(
                'targetId'=>$projectId,
                'userRole'=>$this->user['userRole'],
                'userId' => $this->user['userId'],
                'auditRs' => $this->sProjectModel->getBudgetStatus($projectId),
                'auditDes' => '项目明细结算审核'
            ));
            showSuccess('审核成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择主线序号');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择项目');
            $this->viewData['projectId'] = $projectId;
            //项目模型
        
            $this->viewData['projectData'] = $this->sProjectModel->getNewData(array('id'=>$projectId));
            $this->viewData['projectData'] || showError('项目数据不存在');
            $this->viewData['moneyDetail'] = $this->sProjectModel->getDetailMoney($projectId);
            //线上广宣
            $this->viewData['gxList'] = $this->sPGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'isSettle'=>1,'settleApply >'=>0));
            //线下活动
            $this->viewData['offlineList'] = $this->sPOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'isSettle'=>1,'settleApply >'=>0));
            //查看标记
            $this->viewData['action'] = 'audit';
            $this->load->view('SingleProject/settleAudit', $this->viewData);
        }
    }
    /**
     * 百县强基-修改金额
     */
    function settleChange(){
        $this->load->model('sProjectModel');
        $this->load->model('sPGxModel');
        $this->load->model('sPOfflineModel');
        if ($_POST){
            $projectId = (int)$this->input->post('projectId');
            $projectId || showError('请选择项目');
            $gxData = $this->input->post('gx');
            $offlineData = $this->input->post('offline');
            if (!$gxData && !$offlineData){
                showError('您的提交的数据为空');
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    $this->sPGxModel->edit((int)$k, array(
                        'settleApply' => (float)$v['settleApply'],
                        'settleAudit' => (int)$v['settleAuditRs'] == 1 ? (float)$v['settleApply'] : 0,
                    ));
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->sPOfflineModel->edit((int)$k, array(
                        'settleApply' => (float)$v['settleApply'],
                        'settleAudit' => (int)$v['settleAuditRs'] == 1 ? (float)$v['settleApply'] : 0,
                    ));
                }
            }
            $this->sProjectModel->settleUpdateStatus($projectId);
            $this->sProjectModel->updateMoney($projectId);
            showSuccess('保存成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择主线序号');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择项目');
            $this->viewData['projectId'] = $projectId;
            //项目模型
            $this->viewData['projectData'] = $this->sProjectModel->getNewData(array('id'=>$projectId));
            $this->viewData['projectData'] || showError('项目数据不存在');
            $this->viewData['moneyDetail'] = $this->sProjectModel->getDetailMoney($projectId);
            //线上广宣
            $this->viewData['gxList'] = $this->sPGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'isSettle'=>1,'settleApply >'=>0));
            //线下活动
            $this->viewData['offlineList'] = $this->sPOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'isSettle'=>1,'settleApply >'=>0));
            //查看标记
            $this->viewData['action'] = 'change';
            $this->load->view('SingleProject/settleSubmit', $this->viewData);
        }
    }
}